package com.dataextend.controller;

import com.alibaba.fastjson.JSONObject;
import com.dataextend.dto.PgMessageResult;
import com.dataextend.dto.ResultSegment;
import com.dataextend.entity.*;
import com.dataextend.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  事件分析模型
 */
@RestController
@RequestMapping("/v1/DataAnalysis")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class AnalysisModeController {

    @Autowired
    private EventListService eventListService;
    @Autowired
    private ClickhouseRemainService clickhouseRemainService;
    @Autowired
    private ClickhouseEventAnalysisService clickhouseEventAnalysisService;
    @Autowired
    private ClickhouseHopperAnalysisService clickhouseHopperAnalysisService;
    @Autowired
    private ClickhouseDistributeService clickhouseDistributeService;


    /**
     * 事件选项下拉菜单
     * @return
     */
    @RequestMapping(value = "/eventList", method = RequestMethod.POST)
    public PgMessageResult GetEventList(){
        List<JSONObject> eventLists = eventListService.eventList();
        return PgMessageResult.success(eventLists);
    }


    /**
     * 细分维度[设备属性/用户属性] 下拉菜单
     * @return
     */
    @ApiOperation("细分维度选项-下拉菜单")
    @RequestMapping(value = "/attributeList", method = RequestMethod.POST)
    public PgMessageResult GetattributeList(){
        List<JSONObject> attributeList = eventListService.attributeList();
        return PgMessageResult.success(attributeList);
    }


    /**
     * 事件分析模型 clickhouse
     */
    @ApiOperation(value = "事件分析模型clickhouse")
    @RequestMapping(value = "/clickhouse/eventAnalysis", method = RequestMethod.POST)
    public ResultSegment clickhouseEventAnalysis(@RequestBody EventAnalysisRequest eventAnalysisRequest) {
        if (eventAnalysisRequest.getStartDate() == null || eventAnalysisRequest.getEndDate() ==null) {
            return new ResultSegment(false, 2000, "起止日期为必填参数");
        }
        JSONObject map = clickhouseEventAnalysisService.findCount(eventAnalysisRequest);
        if (map != null) {
            return new ResultSegment(true, 1, "查询成功", map);
        }
        return new ResultSegment(false, 400, "程序异常");
    }


    /**
     * 留存分析模型 clickhouse
     */
    @ApiOperation(value = "留存分析模型clickhouse")
    @RequestMapping(value = "/clickhouse/remainAnalysis", method = RequestMethod.POST)
    public ResultSegment clickhouseRemainremainAnalysis(@RequestBody RequestRemainAnalysis requestRemainAnalysis){
        JSONObject resultArray = clickhouseRemainService.reaminAnalysis(requestRemainAnalysis);
        if (resultArray != null){
            return new ResultSegment(true, 1, "查询成功", resultArray);
        }
        return new ResultSegment(false, 400, "程序异常");
    }



    /**
     * 分布分析模型 clickhouse
     */
    @ApiOperation(value = "分布分析模型clickhouse")
    @RequestMapping(value = "/clickhouse/distributeAnalysis", method = RequestMethod.POST)
    public ResultSegment distributeAnalysis(@RequestBody DistributeAnalysisRequest distributeAnalysisRequest){
        JSONObject returnResult = clickhouseDistributeService.findCound(distributeAnalysisRequest);
        if (returnResult != null){
            return new ResultSegment(true,1,"查询成功", returnResult);
        }
        return new ResultSegment(false,400,"程序异常");
    }



    /**
     * 转化漏斗分析模型 clickhouse
     */
    @ApiOperation(value = "转化漏斗分析模型clickhouse")
    @RequestMapping(value = "/clickhouse/hopperAnalysis", method = RequestMethod.POST)
    public ResultSegment clickhouseHopperAnalysis(@RequestBody HopperAnalysisRequest hopperAnalysisRequest){
        JSONObject map = clickhouseHopperAnalysisService.hopperAnalysis(hopperAnalysisRequest);

        if (map != null){
            return new ResultSegment(true, 1, "查询成功", map);
        }
        return new ResultSegment(true, 400, "程序异常");
    }


    /**
     * 下拉菜单过滤条件Key
     * @return
     */
    @RequestMapping(value = "/dropDownListKey", method = RequestMethod.POST)
    public ResultSegment dropDownListKey() {
        JSONObject map =  eventListService.dropDownListKey();
        return new ResultSegment(true, 1, "查询成功", map);
    }


    /**
     * 下拉菜单过滤条件Value
     * @param level_2_name
     * @return
     */
    @RequestMapping(value = "/dropDownListValue", method = RequestMethod.POST)
    public ResultSegment dropDownListValue(@RequestParam(value = "level_2_name", required = false) String level_2_name) {
        JSONObject map =  eventListService.dropDownListValue(level_2_name);
        return new ResultSegment(true, 1, "查询成功", map);
    }


    /**
     * 下拉菜单过滤条件
     * @return
     */
    @ApiOperation(value = "公共过滤条件")
    @RequestMapping(value = "/dropDownList", method = RequestMethod.POST)
    public ResultSegment dropDownList() {
        JSONObject map =  eventListService.dropDownList();
        return new ResultSegment(true, 1, "查询成功", map);
    }

}
